//
//  CommonBusinessResponse.m
//  EUCIOS
//
//  Created by Khang on 4/10/13.
//  Copyright (c) 2013 Khang. All rights reserved.
//

#import "CommonBusinessResponse.h"
#import "CommonBusinessModel.h"


//Note for future
//        NSData* compressedData = [request rawResponseData];
//        NSData* unCompressedData = [request responseData];

@interface CommonBusinessResponse (NotEditing)
- (void)parseResponse:(NSString*)jsonString;
@end


@interface CommonBusinessResponse (CanEditting)
- (void)parseSignUp:(NSMutableDictionary*)jsonDic;
- (void)parseSignIn:(NSMutableDictionary*)jsonDic;
@end


@implementation CommonBusinessResponse

- (id)init {
    if (self = [super init]) {
        self.errorCode = kCommonBusinessErrorCodeNone;
        self.dataSet = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc {
    if (_dataSet) {
        [_dataSet removeAllObjects];
    }
    RELEASE(_dataSet);
    
    [super dealloc];
}

#pragma mark -
#pragma mark Private
- (void)asiRequestBegin:(ASIHTTPRequest*)request {
    
}

- (void)asiRequestFinish:(ASIHTTPRequest*)request {    
    if ([request isResponseCompressed]) {
        NSString* unCompressedString = [request responseString];
        [self parseResponse:unCompressedString];
        
    } else {
        NSString* responseString = [request responseString];
        [self parseResponse:responseString];
    }
    
    if (self.commonModel && self.commonModel.delegate &&
        [self.commonModel.delegate respondsToSelector:@selector(model:didRequestDataset:)])
    {
        [self.commonModel.delegate model:self.commonModel didRequestDataset:self.dataSet];
    }
    
    if (self.commonModel && self.commonModel.delegate &&
        [self.commonModel.delegate respondsToSelector:@selector(model:didRequestWithErrorCode:)]) {
        [self.commonModel.delegate model:self.commonModel didRequestWithErrorCode:self.errorCode];
    }
}

- (void)asiRequestFail:(ASIHTTPRequest*)request {
    NSError* error = [request error];
    NSLog(@"+ ERROR: %@", error);
    
    if (self.commonModel && self.commonModel.delegate &&
        [self.commonModel.delegate respondsToSelector:@selector(model:didFailWithError:)])
    {
        [self.commonModel.delegate model:self.commonModel didFailWithError:error];
    }
}


////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// JUST DEFINE BELOW ///////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////

#pragma mark -
#pragma mark Implement
- (void)parseResponse:(NSString *)jsonString {
    NSLog(@"+ RESPONSE: %@", jsonString);
    
    //make json object
    NSMutableDictionary* jsonDic = [jsonString mutableObjectFromJSONString];
    
    //get errorCode
    self.errorCode = [[Common sharedCommon] objectToInterger:[jsonDic objectForKey:@"error_code"]];
    
    //parse json string base on type
    switch (self.commonBusinessType) {
        case kCommonBusinessTypeSignup: {
            [self parseSignUp:jsonDic];
        }
            break;
        case kCommonBusinessTypeSignin: {
            [self parseSignIn:jsonDic];
        }
            break;
        default:
            break;
    }
}

#pragma mark -
#pragma mark Public
- (void)parseSignUp:(NSMutableDictionary *)jsonDic {
    [self.dataSet removeAllObjects];
    
    if (jsonDic && [jsonDic isKindOfClass:[NSDictionary class]]) {
        NSDictionary* data = [jsonDic objectForKey:@"data"];
        if (data && [data isKindOfClass:[NSDictionary class]]) {
            if ([data objectForKey:@"user_id"]) {
                [self.dataSet addObject:[[Common sharedCommon] objectToString:[data objectForKey:@"user_id"]]];
            }
        }
    }
}

- (void)parseSignIn:(NSMutableDictionary *)jsonDic {
    [self.dataSet removeAllObjects];
    
    NSDictionary* data = [jsonDic objectForKey:@"data"];
    if (data && [data isKindOfClass:[NSDictionary class]]) {
        if ([data objectForKey:@"session_key"]) {
            [self.dataSet addObject:[[Common sharedCommon] objectToString:[data objectForKey:@"session_key"]]];
        }
    }
}

@end
